<template>
  <el-form
    label-position="right"
    label-width="100px"
    :model="userForm"
    :rules="rules"
    ref="userFormRef"
  >
    <el-form-item label="用户名" prop="username">
      <el-input v-model="userForm.username" />
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-select
        v-model="userForm.gender"
        class="m-2"
        placeholder="Select"
        style="width: 100%"
      >
        <el-option
          v-for="item in genderOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="个人简介" prop="introduction">
      <el-input v-model="userForm.introduction" type="textarea" />
    </el-form-item>
    <el-form-item label="头像">
      <el-upload
        class="avatar-uploader"
        :on-change="onChangeAvatar"
        :show-file-list="false"
        :auto-upload="false"
      >
        <img v-if="userForm.avatar" :src="userForm.avatar" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="submitUserForm()">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import axios from "axios";
import { Plus } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import LocalCache from "@/utils/cache";
let userInfo = LocalCache.getStorage("userInfo");
// 收集用户信息
let userForm = reactive({
  username: userInfo.username ?? "", //用户名
  gender: userInfo.gender ?? "", //性别
  introduction: userInfo.introduction ?? "", //简介
  avatar: userInfo.avatar ?? "",
  avtarFile: null, //头像文件
});
let genderOptions = reactive([
  {
    label: "男",
    value: 1,
  },
  {
    label: "女",
    value: 2,
  },
  {
    label: "保密",
    value: 0,
  },
]);
let userFormRef = ref();
// 校验规则
let rules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  gender: [{ required: true, message: "请选择性别", trigger: "blur" }],
  introduction: [{ required: true, message: "请输入简介", trigger: "blur" }],
});
// 上传头像的回调
const onChangeAvatar = (file) => {
  userForm.avatar = URL.createObjectURL(file.raw);
  userForm.avtarFile = file.raw;
};
// 更新用户信息的请求
const updateRequest = () => {
  // 创建fromData对象
  const params = new FormData();
  for (let key in userForm) {
    params.append(key, userForm[key]);
  }
  // 请求头要设置为multipart/form-data 格式
  axios.post("/adminapi/user/update", params, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};
// 更新用户信息
const submitUserForm = () => {
  // 校验
  userFormRef.value.validate((isValidate) => {
    if (isValidate) {
      console.log("校验通过");
      updateRequest();
    } else {
      console.log("校验不通过");
    }
  });
};
</script>

<style lang="scss" scoped>
.el-form {
  padding: 12px;
}
// 头像传样式
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

::v-deep .avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
